<style>
    .layui-form-item .layui-input-company {
        width: auto;
        padding-right: 10px;
        line-height: 38px;
    }
</style>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label required">旧的密码</label>
                <div class="layui-input-block">
                    <input type="password" name="old_password" id="old_password" lay-verify="required" lay-reqtext="旧的密码不能为空" placeholder="请输入旧的密码" value="" class="layui-input">
                    <tip>填写自己账号的旧的密码。</tip>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">新的密码</label>
                <div class="layui-input-block">
                    <input type="password" name="new_password" id="new_password" lay-verify="required" lay-reqtext="新的密码不能为空" placeholder="请输入新的密码" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">新的密码</label>
                <div class="layui-input-block">
                    <input type="password" name="again_password" id="again_password" lay-verify="required" lay-reqtext="新的密码不能为空" placeholder="请输入新的密码" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['layer','form','jquery', 'miniPage'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            miniPage = layui.miniPage;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let oldPwd = document.getElementById("old_password").value;;
            let newPwd = document.getElementById("new_password").value;;
            let againPwd = document.getElementById("again_password").value;;

            if (newPwd === againPwd)
            {
                if (oldPwd === newPwd)
                {
                    layer.msg("新旧密码相同，请重新输入：",{icon:2,time:1000},function () {
                        // window.location.reload();
                    });
                }else{
                    $.ajax({
                        url:'/User/pwdSubmit',
                        method:'POST',
                        data:{
                            'oldPwd':oldPwd,
                            'newPwd':newPwd
                        },
                        success:function (res) {
                            if (res.code == 200){
                                layer.msg(res.msg,{icon:1,time:1500});
                                miniPage.hashHome();
                            }else {
                                layer.msg(res.msg,{icon:2,time:1000});
                            }
                        },
                        error:function () {
                            window.location.href="/User/404";
                        }
                    });

                }

            }else {
                layer.msg("新密码两次输入不同，请重新输入：",{icon:2,time:1000},function () {
                   // window.location.reload();
                });
            }


            return false;
        });

    });
</script>